package es.ucm.fdi.isbc.recomendador.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class URLRenderer extends DefaultTableCellRenderer implements MouseListener, MouseMotionListener {

	private static final long serialVersionUID = 7094495877285232673L;
	private int row = -1;
	private int col = -1;
	private boolean isRollover = false;
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
	{
		super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
		if(!table.isEditing() && this.row==row && this.col==column && this.isRollover) {
		}else if(hasFocus) {
			URL url;
			try {
				url = new URL(value.toString());
				Desktop.getDesktop().browse(url.toURI());
				//Se selecciona otra celda para dejar de abrir URl en navegador.
			table.setRowSelectionInterval(row, row);
			table.setColumnSelectionInterval(column+2, column+2);
		} catch (MalformedURLException e) {
			System.out.println("URL mal formada.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Problema URL.");
			e.printStackTrace();
		} catch (URISyntaxException e) {
			System.out.println("Problema URL.");
				e.printStackTrace();
			}
		}else{
			if (value.toString().startsWith("http://images"))
		setText("Ver foto");
		else 
			setText("Ver anuncio");
			this.setBackground(Color.LIGHT_GRAY);
			this.setForeground(Color.BLUE);
		}
		return this;
	}
	@Override 
	public void mouseMoved(MouseEvent e) {
	   
	}
	  
	@Override 
	public void mouseExited(MouseEvent e)  {
	   
	}
	  
	@Override 
	public void mouseClicked(MouseEvent e) {
	   
	}
	  
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		
	}
}